package xh.server.processors;

import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.commons.io.IOUtils;
import xh.util.Global;
import xh.util.HttpUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;

/**
 * Created by Administrator on 2016/6/25.
 */
public class StaticResourceProcessor extends AbstractProcessor {

    private String uri;

    public StaticResourceProcessor(String s) {
        this.uri = s;
    }

    public FullHttpResponse process() {
        URL staticFile = Thread.currentThread().getContextClassLoader().getResource("statics" + "/" + uri);
        try {
            byte[] content = IOUtils.toByteArray(staticFile.openStream());
            FullHttpResponse response = defaultResponse(content);
            response.headers().set(CONTENT_TYPE, "text/html");
            return response;

        } catch (IOException e) {
            e.printStackTrace();
            return HttpUtils._404();
        }
    }


    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }
}
